Some side effects need cleanup for example Component A requests the API to get some data, but while making that asynchronous request, Component A is removed from the DOM (it’s unmounted). There is no need to complete that asynchronous request. We can clean up (cancel) the asynchronous request so that it’s not completed.
the cleanup function helps developers clean effects that prevent unwanted behaviours and optimize application performance.
It is a function of the useEffect hook that allows us to stop side effects that no longer need to be executed before our component is unmounted.
useEffect is built in such a way that we can return a function inside it and this return function is where the cleanup happens.